<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: IndexAction.class.php 31 2011-09-26 01:35:37Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class OptionsAction extends AdminBaseAction {
	
	/**
	 * Options 模型句柄
	 *
	 * @access public
	 * @var string
	 */
	public $dao;
	
	/**
	 * 初始化
	 *
	 * @access public
	 * @return void
	 */
	public function _initialize() {
		
		parent::_initialize ();
		
		$this->dao = D ( 'Options' );
	}
	
	/**
	 * 常规选项
	 *
	 * @access public
	 * @return void
	 */
	public function general() {
		
		$data = $this->r->from ( 'title', 'description', 'website', 'register', 'role', 'timezone', 'dateFormat', 'timeFormat' );
		
		if (! empty ( $this->r->action ) && $this->r->action == 'general') {
			
			$data ['register'] = empty ( $data ['register'] ) ? 0 : 1;
			
			/** 验证规则 */
			$this->v->addRule ( 'title', 'required', '错误：必须填写站点名称' );
			$this->v->addRule ( 'website', 'required', '错误：必须填写站点地址' );
			$this->v->addRule ( 'website', 'url', '错误：您输入的站点地址可能有误，请输入一个正确的 URL。' );
			
			/** 截获验证异常 */
			if ($row = $this->v->run ( $data )) {
				$this->assign ( 'error', $this->getErrorNotice ( $row ) );
			} else {
				$this->dao->setOptions ( $data );
				/** 刷新缓存 */
				S ( 'options', NULL );
				$this->assign ( 'success', '设置已保存。' );
			}
		}
		
		$this->assign ( 'ur_here', array ('text' => '常规选项', 'action' => 'options-general' ) );
		$this->assign ( 'options', $this->dao->getOptions () );
		$this->display ();
	}
	
	/**
	 * 邮件设置
	 *
	 * @access public
	 * @return void
	 */
	public function email() {
		
		$data = $this->r->from ( 'mailService', 'smtpHost', 'smtpPort', 'smtpSecure', 'smtpUser', 'smtpPass', 'smtpCharset', 'smtpFromTo', 'smtpFromName' );
		
		if (! empty ( $this->r->action ) && $this->r->action == 'email') {
			
			$this->v->addRule ( 'smtpPort', 'isInteger', '错误：SMTP 服务器端口通常是一个数字，例如：25' );
			$this->v->addRule ( 'smtpUser', 'email', '错误：SMTP 帐号可能有误' );
			
			if ($row = $this->v->run ( $data )) {
				$this->assign ( 'error', $this->getErrorNotice ( $row ) );
			} else {
				$this->dao->setOptions ( $data );
				S ( 'options', NULL );
				$this->assign ( 'success', '设置已保存。' );
			}
		}
		
		$this->assign ( 'ur_here', array ('text' => '邮件设置', 'action' => 'options-general' ) );
		$this->assign ( 'options', $this->dao->getOptions () );
		$this->display ();
	}
	
	/**
	 * 邮件设置测试接口
	 *
	 * @access public
	 * @return void
	 */
	public function send() {
		
		$data = $this->r->from ( 'test' );
		$this->v->addRule ( 'test', 'required', '错误：必须填写测试邮件地址' );
		$this->v->addRule ( 'test', 'email', '错误：请填写正确的测试邮件地址' );
		
		if ($row = $this->v->run ( $data )) {
			$this->ajaxReturn ( '', $this->getErrorNotice ( $row ), 0 );
		} else {
			import ( '@.ORG.Email' );
			$content = '您好!这是一封检测邮件服务器设置的测试邮件。<br />收到此邮件,意味着您的邮件服务器设置正确！';
			$subject = '[' . C ( 'title' ) . '] 测试邮件';
			if (Email::send ( $data ['test'], $subject, $content, 1 )) {
				$this->ajaxReturn ( '', '测试邮件已成功发送到 ' . $data ['test'], 1 );
			} else {
				$this->ajaxReturn ( '', '错误：邮件发送失败,请检查邮件服务器设置。', 0 );
			}
		}
	}
	
	/**
	 * 隐私设置
	 *
	 * @access public
	 * @return void
	 */
	public function privacy() {
		
		$data = $this->r->from ( 'blogPublic' );
		
		if (! empty ( $this->r->action ) && $this->r->action == 'privacy') {
			
			$this->dao->setOptions ( $data );
			
			/** 生成robots.txt文件 */
			do_robots ();
			
			/** 刷新缓存 */
			S ( 'options', NULL );
			$this->assign ( 'success', '设置已保存。' );
		}
		
		$this->assign ( 'ur_here', array ('text' => '隐私设置', 'action' => 'options-general' ) );
		$this->assign ( 'options', $this->dao->getOptions () );
		$this->display ();
	}

}